﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Video;
using System.Threading;

namespace TestVideoBase
{
    public partial class Form1 : Form
    {
        Dictionary<string, List<string>> devices;
        string area = string.Empty;
        string device = string.Empty;

        public Form1()
        {
            devices = VideoBase.GetAreaDeviceList();

            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            foreach (var area in devices)
            {
                TreeNode TN = new TreeNode(area.Key);
                foreach (var device in area.Value) TN.Nodes.Add(device);
                this.tVDevice.Nodes.Add(TN);
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            if (video.IsInitialized)
            {
                video.CloseVideo();
                video.LogoutEncoder();
            }

            base.OnClosed(e);
        }

        private void tVDevice_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (tVDevice.SelectedNode != null
                && tVDevice.SelectedNode.Parent != null)
            {
                string area = tVDevice.SelectedNode.Parent.Text;
                string device = tVDevice.SelectedNode.Text;

                if (!video.IsInitialized)
                {
                    this.area = area;
                    this.device = device;
                    video.Initialize(area, device);
                }

                video.LoginEncoder();
                video.OpenVideo();
            }
        }

        private void bUp_MouseDown(object sender, MouseEventArgs e)
        {
            if (video.TiltUp(Convert.ToByte(hSBSpeed.Value)))
            {
                isStop = false;
                isGetVertical = true;
                tAngle.Start();
            }
        }

        private void bDown_MouseDown(object sender, MouseEventArgs e)
        {
            if (video.TiltDown(Convert.ToByte(hSBSpeed.Value)))
            {
                isStop = false;
                isGetVertical = true;
                tAngle.Start();
            }
        }

        private void bLeft_MouseDown(object sender, MouseEventArgs e)
        {
            if (video.PanLeft(Convert.ToByte(hSBSpeed.Value)))
            {
                isStop = false;
                isGetHorizon = true;
                tAngle.Start();
            }
        }

        private void bRight_MouseDown(object sender, MouseEventArgs e)
        {
            if (video.PanRight(Convert.ToByte(hSBSpeed.Value)))
            {
                isStop = false;
                isGetHorizon = true;
                tAngle.Interval = 800 - hSBSpeed.Value * 100;
                tAngle.Start();
            }
        }

        bool isStop = true;
        private void bPT_MouseUp(object sender, MouseEventArgs e)
        {
            video.PTZStop();
            isStop = true;
        }

        private void bHorizon_Click(object sender, EventArgs e)
        {
            video.AsyncGetHorizon();
        }

        private void bVertical_Click(object sender, EventArgs e)
        {
            video.AsyncGetVertical();
        }

        delegate void HorizonCallback(object sender, IVideo.HorizonEventArgs e);
        private void video_HorizonCallback(object sender, IVideo.HorizonEventArgs e)
        {
            if (this.InvokeRequired)
                this.Invoke(new HorizonCallback(video_HorizonCallback), new object[] { sender, e });
            else
            {
                if (nUDHorizon.Value == e.Horizon) isGetHorizon = false;
                else if (isStop)video.PTZStop();

                nUDHorizon.Value = e.Horizon;
            }
        }

        delegate void VerticalCallback(object sender, IVideo.VerticalEventArgs e);
        private void video_VerticalCallback(object sender, IVideo.VerticalEventArgs e)
        {
            if (this.InvokeRequired)
                this.Invoke(new VerticalCallback(video_VerticalCallback), new object[] { sender, e });
            else
            {
                if (nUDVertical.Value == e.Vertical) isGetVertical = false;
                else if (isStop) video.PTZStop();

                nUDVertical.Value = e.Vertical;
            }
        }

        private void bGoHorizon_Click(object sender, EventArgs e)
        {
            video.GoHorizon((int)nUDHorizon.Value);
        }

        private void bGoVertical_Click(object sender, EventArgs e)
        {
            video.GoVertical((int)nUDVertical.Value);
        }

        bool isGetHorizon = false, isGetVertical = false;
        private void tAngle_Tick(object sender, EventArgs e)
        {
            if (isGetHorizon) video.AsyncGetHorizon();
            if (isGetVertical) video.AsyncGetVertical();

            if (!isGetHorizon && !isGetVertical)
                tAngle.Stop();
        }
    }
}
